package com.fc.mall.service.impl;

import com.fc.mall.dao.PmsGroupBuyDao;
import com.fc.mall.dto.GroupPromotionParam;
import com.fc.mall.dto.GroupPromotionRequest;
import com.fc.mall.dto.PmsGroupBuy;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.SmsGroupbuyPromotionMapper;
import com.fc.mall.model.SmsGroupbuyPromotion;
import com.fc.mall.model.SmsGroupbuyPromotionExample;
import com.fc.mall.service.SmsGroupPromotionService;
import com.github.pagehelper.PageHelper;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by sunny on 2019/10/12.
 */
@Service
public class SmsGroupPromotionServiceImpl implements SmsGroupPromotionService {

    @Autowired
    private SmsGroupbuyPromotionMapper groupbuyPromotionMapper;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private PmsGroupBuyDao pmsGroupBuyDao;


    @Override
    public List<SmsGroupbuyPromotion> getList(GroupPromotionParam groupPromotionParam, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        SmsGroupbuyPromotionExample promotionExample = new SmsGroupbuyPromotionExample();
        SmsGroupbuyPromotionExample.Criteria criteria = promotionExample.createCriteria();
        if(!StringUtils.isEmpty(groupPromotionParam.getName()))
        {
            criteria.andNameLike("%"+groupPromotionParam.getName()+"%");
        }
        promotionExample.setOrderByClause("id desc");

        List<SmsGroupbuyPromotion> smsGroupbuyPromotionList = groupbuyPromotionMapper.selectByExample(promotionExample);
        smsGroupbuyPromotionList.stream().forEach(v->{
            calculatePayOrderCount(v);
        });
        return smsGroupbuyPromotionList;
    }

    @Override
    public int createOrUpdateGroupPromotin(GroupPromotionRequest groupPromotionRequest) {
        //开始时间或结束时间存在空
        if (null == groupPromotionRequest.getStartDate() ||
                null == groupPromotionRequest.getEndDate()){
            throw new ServiceException("开始时间、结束时间不可为空！");
        }
        long startTime = groupPromotionRequest.getStartDate().getTime();
        long endTime = groupPromotionRequest.getEndDate().getTime();
        long now = System.currentTimeMillis();
        if (endTime <= startTime || endTime <= now) {
            throw new ServiceException("结束时间应大于开始时间和当前时间！");
        }


        SmsGroupbuyPromotion groupbuyPromotion = new SmsGroupbuyPromotion();
        mapperFacade.map(groupPromotionRequest, groupbuyPromotion);
        if(null == groupbuyPromotion.getId())
        {
            return groupbuyPromotionMapper.insertSelective(groupbuyPromotion);
        }
        SmsGroupbuyPromotion oldPromotion = groupbuyPromotionMapper.selectByPrimaryKey(groupbuyPromotion.getId());
        if(null != oldPromotion)
        {
            return groupbuyPromotionMapper.updateByPrimaryKeySelective(groupbuyPromotion);
        }
        throw new ServiceException("当前砍价活动不存在");
    }

    @Override
    public SmsGroupbuyPromotion getGroupPromotionById(Long id) {

        return groupbuyPromotionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delGroupPromotionById(Long id) {
        return groupbuyPromotionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PmsGroupBuy> getPromotionProductList(Long pid, Integer pageNum, Integer pageSize) {
        if(null != pageNum && null != pageSize)
        {
            PageHelper.startPage(pageNum,pageSize);
        }
        return pmsGroupBuyDao.queryGroupBuyByPromotionId(pid);
    }

    @Override
    public int addProductForPromotion(Long promotionId, Long pid) {

        Long count = pmsGroupBuyDao.getCountByPidFromPromotion(promotionId, pid);
        if(count.intValue() > 0)
        {
            throw new ServiceException("当前团购商品已存在");
        }
        return pmsGroupBuyDao.addProductForPromotion(promotionId, pid);
    }

    @Override
    public int removeProductFromPromotion(Long promotionId, Long pid) {
        return pmsGroupBuyDao.removeProductForPromotion(promotionId, pid);
    }


    private void calculatePayOrderCount(SmsGroupbuyPromotion smsGroupbuyPromotion)
    {
       List<PmsGroupBuy> pmsGroupBuyList = this.getPromotionProductList(smsGroupbuyPromotion.getId(), null, null);
       List<Long> productGroupIds = pmsGroupBuyList.stream().map(PmsGroupBuy::getId).collect(Collectors.toList());
       if(productGroupIds.isEmpty())
       {
           smsGroupbuyPromotion.setPaymentOrderCount(0);
           smsGroupbuyPromotion.setGroupBuyCount(0);
       }
       else
       {
           Long paymentOrderCount =  pmsGroupBuyDao.calculateGroupBuyOrderCount(productGroupIds);
           Long groupCount = pmsGroupBuyDao.calculateGroupBuyCount(productGroupIds);
           smsGroupbuyPromotion.setPaymentOrderCount(paymentOrderCount.intValue());
           smsGroupbuyPromotion.setGroupBuyCount(groupCount.intValue());
       }
    }
}
